import _Vue from '../../main'
import {Base64} from "js-base64";
import pathData from "@/components/menu";

let common = {
    getPowerList(powerCode) {
        var btnList = [];
        let menuList = _Vue.getStorage("menuList");
        let powerId = _Vue.getStorage("topMenu");
        const menu = menuList.filter(item => item.id == powerId)[0];
        this.getBtnList(menu.childList, powerCode, btnList);
        return btnList;
    },
    getBtnList(menuList, powerCode, btnList) {
        for (var i in menuList) {
            if (menuList[i].powerCode == powerCode) {
                for (var j in menuList[i].btnList) {
                    btnList.push(menuList[i].btnList[j].powerCode);
                }
                break;
            }
            if (menuList[i].childList) {
                this.getBtnList(menuList[i].childList, powerCode, btnList);
            }
        }
    },
    showImage(file){
        return URL.createObjectURL(file);
    },
    uploadFile(formData, exeFile) { //上传文件
        _Vue.$axios.post("/CHILD_SYSTEM/baseUploadFile/upload", formData).then(res => {
            exeFile(res);
        }).catch(err => {
            exeFile(err);
        })
    },
    downloadData(res) {
        if (!res.fileinfo) return _Vue.$message.error("导出失败，请检查网络连接是否成功！");
        let info = JSON.parse(res.fileinfo);
        const blob = new Blob([res], {type: info.fileType}); // 构造一个blob对象来处理数据，并设置文件类型
        const fileName = Base64.decode(info.fileName);
        if (window.navigator.msSaveOrOpenBlob) { //兼容IE10
            navigator.msSaveBlob(blob, fileName);
        } else {
            const href = URL.createObjectURL(blob); //创建新的URL表示指定的blob对象
            const a = document.createElement('a') //创建a标签
            a.style.display = 'none'
            a.href = href // 指定下载链接
            a.download = fileName; //指定下载文件名
            a.click(); //触发下载
            URL.revokeObjectURL(a.href);  //释放URL对象
        }
    },
    downloadFile(id) {//下载文件流
        _Vue.$axios.get("/CHILD_SYSTEM/baseUploadFile/download/" + id, null, 'arraybuffer').then(res => {
            this.downloadData(res)
        }).catch(err => {
            _Vue.$message.error(err.message)
        })
    },
    downloadFileSystem(fileCode) {//下载文件流操作手册和浏览器
        _Vue.$axios.get("/CHILD_SYSTEM/baseUploadFile/downloadFile/" + fileCode, null, 'arraybuffer').then(res => {
            this.downloadData(res)
        }).catch(() => {
            _Vue.$message.error(err.message)
        })
    },
    getDicList(typeCode) { //获取数据字典列表
        let list = _Vue.getStorage('dicList');
        return list?.[typeCode];
    },
    getDicName(typeCode, dictionaryId) { //获取数据字典名
        if ("yesNo" == typeCode) {
            return dictionaryId == 1 ? "是" : "否";
        } else {
            let list = _Vue.getStorage('dicMap');
            return list?.[typeCode]?.[dictionaryId];
        }
    },
    getChinaRegionList() { //中国地区
        let list = _Vue.getStorage('worldRegionList');
        let chinaRegion;
        list.forEach( temp => {
            if (temp.value == 1) {
                temp.childNode.forEach( item => {
                    if (item.value == 56) {
                        chinaRegion = item.childNode
                    }
                })
            }
        })
        return chinaRegion || [];
    },
    getChinaRegionName(id) { //获取中国地区名
        let list = _Vue.getStorage('chinaRegionMap');
        return list?.[id];
    },
    getIndustryTypeList() { //行业分类
        let list = _Vue.getStorage('industryTypeList');
        return list || [];
    },
    getIndustryTypeMap(code) { //获取行业分类名
        let list = _Vue.getStorage('industryTypeMap');
        return list?.[code];
    },
    getSelectList(rows, label, value, topBoolean) { //组装下拉列表数据
        let list = [];
        if (topBoolean) {
            list.push({value: '0', label: '顶级类型'});
        }
        rows.forEach(temp => {
            list.push({
                label: temp[label],
                value: temp[value],
                data: temp,
            })
        });
        return list;
    },
    openDetail (query, name) {
        const {href} = _Vue.$router.resolve({
            name: name,
            query: query
        })
        window.open(href, '_blank')
    },
    getPowerPath(menuList){
        console.log(menuList)
        if (menuList.length > 0) {
            const menu = menuList[0];
            if (menu.isLeaf == 1) {
                return menu.powerPath;
            } else {
                return this.getPowerPath(menu.childList);
            }
        }
    }
}
export default common;
